package com.xfp.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xfp.mapper.TaskMapper;
import com.xfp.pojo.dto.CancelTaskDto;
import com.xfp.pojo.dto.TaskInfoDto;
import com.xfp.pojo.dto.TaskQueryDto;
import com.xfp.pojo.entity.*;
import com.xfp.pojo.vo.*;
import com.xfp.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    /**
     * 统计工单状态
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<CollectReportVo> collectReport(String start, String end) {
        List<CollectReportVo> list =  taskMapper.collectReport(start,end);
        return list;
    }

    /**
     * 获取当时工单汇总信息(人员统计头部信息)
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<TaskReportInfoVo> taskReportInfo(String start, String end) {
        List<TaskReportInfoVo> list =  taskMapper.taskReportInfo(start,end);
        if (list.size() == 0){
            return null;
        }
        list.forEach(l->{
            l.setWorkerCount(1);
            l.setRepair(false);
            l.setDate(start+end);
        });
        return list;
    }

    /**
     * 工单搜索
     * @param dto
     * @return
     */
    @Override
    public TaskQueryVo taskSearch(TaskQueryDto dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        //查询工单基础信息
        Page<Task> page = taskMapper.taskSearch(dto);
        if(page.getResult()==null){
            return null;
        }
        List<Integer> typeIds = page.getResult().stream().map(t ->t.getTaskStatus()).collect(Collectors.toList());
        //查询工单类型详情并补全
        List<TaskType> taskTypesList = taskMapper.taskTypeSearch(typeIds);
        IntStream.range(0,page.getResult().size()).forEach(i->{
            page.getResult().get(i).setTaskType(taskTypesList.stream().filter(taskType -> taskType.getTypeId().equals(page.getResult().get(i).getTaskStatus())).collect(Collectors.toList()).get(0));
        });
        return new TaskQueryVo(dto.getPageIndex(),dto.getPageSize(),page.getTotal(), (long) page.getPageNum(),page.getResult());
    }

    /**
     * 工单状态列表
     * @return
     */
    @Override
    public List<TaskStatusType> allTaskStatus() {
        List<TaskStatusType> list = taskMapper.allTaskStatus();
        return list;
    }

    /**
     * 获取所有工单类型
     * @return
     */
    @Override
    public List<TaskType> taskTypeList() {
        List<TaskType> list = taskMapper.taskTypeList();
        return list;
    }

    /**
     * 根据工单id获取工单详情
     * @param taskId
     * @return
     */
    @Override
    public TaskInfoVo taskInfo(Long taskId) {
        TaskInfoVo vo = taskMapper.taskInfo(taskId);
        List<TaskDetails> detailsList = taskMapper.taskDetailsList(taskId);
        return vo;
    }

    /**
     * 补货详情
     * @param taskId
     * @param dto
     * @return
     */
    @Override
    public List<TaskDetails> taskDetails(Long taskId, TaskInfoDto dto) {
        taskMapper.insertTask(taskId,dto);
        taskMapper.insertTaskDetails(taskId,dto);
        List<TaskDetails> list = taskMapper.taskDetailsList(taskId);
        return list;
    }

    /**
     * 设置自动补货工单阈值
     * @param alertValue
     */
    @Override
    public void autoSupplyConfig(AlertValue alertValue) {
        taskMapper.autoSupplyConfig(alertValue);
    }

    /**
     * 获取预警值
     * @return
     */
    @Override
    public Integer getAlertValue() {
        return taskMapper.getAlertValue();
    }

    /**
     * 取消工单
     * @param taskId
     * @param dto
     */
    @Override
    public void cancelTask(Integer taskId, CancelTaskDto dto) {
        taskMapper.cancelTask(taskId,dto);
    }

    /**
     * 创建工单
     * @param dto
     */
    @Override
    public void createTask(TaskInfoDto dto) {
        String taskCode = String.valueOf(UUID.randomUUID());
        dto.setTaskCode(taskCode);
        taskMapper.createTask(dto);
        List<TaskDetails> list = dto.getDetails();
        taskMapper.createTaskDetails(list);
    }
}
